package org.surelink.radius.console;

import org.surelink.radius.cache.ClientCache;
import org.surelink.radius.console.message.Request;
import org.surelink.radius.console.message.RespBase;
import org.surelink.radius.console.message.Response;
import org.surelink.radius.constant.ClientTypes;
import org.surelink.radius.model.Client;
import de.tototec.cmdoption.CmdOption;
import de.tototec.cmdoption.CmdlineParser;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Set;

public class ClientProcess extends BaseProcess
{

    @Autowired
    private ClientCache clientCache;

    class Config
    {
        @CmdOption(names = {"--help"}, description = "打印帮助", isHelp = true)
        private boolean help;

        @CmdOption(names = {"client"}, hidden = true)
        private boolean client;

        @CmdOption(names = {"set"}, description = "设置客户端")
        private boolean set;

        @CmdOption(names = {"get"}, description = "查询客户端")
        private boolean get;

        @CmdOption(names = {"list"}, description = "查询所有客户端")
        private boolean list;

        @CmdOption(names = {"--address", "-a"}, args = {"address"}, minCount = -1, maxCount = 1, description = "客户端主机")
        String address;

        @CmdOption(names = {"--type", "-t"}, args = {"type"}, minCount = -1, maxCount = 1,
                description = "客户端类型:huawei/h3c/cisco/ros/other")
        String type;

        @CmdOption(names = {"--secret", "-s"}, args = {"secret"}, minCount = -1, maxCount = 1, description = "客户端共享密钥")
        String secret;
    }


    public ClientProcess()
    {
        setOperate("client");
    }

    @Override
    public Response process(Request req) throws IOException
    {
        Config config = new Config();
        CmdlineParser cp = new CmdlineParser(config);
        cp.setProgramName(getOperate());
        cp.parse(req.getCommand().split("\\s+"));

        if (config.help)
        {
            StringBuilder sb = new StringBuilder();
            cp.usage(sb);
            return new Response(RespBase.SUCCESS.getCode(), sb.toString());
        }

        if (config.set)
        {
            if (config.address == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "客户端地址不能为空");

            Client client = new Client();
            client.setAddress(config.address);
            client.setClientDesc(config.address);
            client.setClientType(config.type != null ? config.type : ClientTypes.OTHER.value());
            client.setSecret(config.secret);
            clientCache.set(client);
            return RespBase.SUCCESS;
        }
        else if (config.get)
        {
            if (config.address == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "客户端地址不能为空");

            Client client = clientCache.get(config.address);

            if (client == null)
                return new Response(RespBase.NOT_EXIST_ERR.getCode(), "客户端不存在");
            else
                return new Response(client);
        }
        else if (config.list)
        {
            List<Client> clients = clientCache.list();
            return new Response(clients);
        }

        return RespBase.MSG_FORMAT_ERR;
    }
}
